<!-- signature.vue -->
<template>
	<view>
		<view v-if="showSign" class="container">
			<view class="custom-title">
				<text class="label-text">{{ label }}</text>
				<signInput  :initialImage="signImage" @signToUrl="handleSign" />
			</view>

		</view>

		<stampSection v-if="showStamp" @stamp-click="handleStampClick" />
		<view class="custom-dateText">
			<text @click="handleDateClick" class="custom-text3">{{ date || '选择日期' }}</text>
		</view>


		<!-- 日期选择弹窗 -->
		<uni-popup ref="datePopup" type="dialog">
			<uni-popup-dialog mode="base" title="选择日期" @confirm="handleDialogConfirm" @close="handleDialogClose">
				<uni-datetime-picker type="date" :value="date" @change="handleDateChange" />
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import signInput from "@/components/am-sign-input/am-sign-input.vue"
	import stampSection from '@/components/order/stamp-section.vue'
	export default {
		components: {
			signInput,
			stampSection
		},
		props: {
			label: {
				type: String,
				default: '签名项'
			},
			signImage: {
				type: String,
				default: ''
			},
			date: {
				type: String,
				default: ''
			},
			showStamp: {
				type: Boolean,
				default: false
			},
			showSign: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				tempDate: ''
			}
		},
		methods: {
			handleSign(e) {
				console.log(e)
				const response = JSON.parse(e.data)
				console.log(response)
				this.$emit('update:signImage', response.data)
			},
			handleDateClick() {
				this.tempDate = this.date
				this.$refs.datePopup.open()
			},
			handleDateChange(value) {
				this.tempDate = value
			},
			handleDialogConfirm() {
				if (this.tempDate) {
					this.$emit('update:date', this.tempDate)
				}
				this.$refs.datePopup.close()
			},
			handleDialogClose() {
				this.tempDate = ''
				this.$refs.datePopup.close()
			}
		}
	}
</script>

<style scoped>
	/* 虚线容器 */
	.dashed-box {
		width: 100%;
		margin: 24rpx auto;
		padding: 12rpx;
		border: 2rpx dashed #333;
		border-radius: 10rpx;
		background: #fff;
		position: relative;

		&+.dashed-box {
			margin-top: 28rpx;
		}
	}

	. .container {
		width: 100%;
		padding-top: 20px;
	}

	.custom-title {
		display: flex;
		align-items: center;
		width: 100%;
	}

	.label-text {
		flex: 0 0 40%;
		/* 占据30%不伸缩 */
		padding-right: 20rpx;
		font-size: 16px;
		text-align: right;
		white-space: nowrap;
	}

	sign-input {
		flex: 1;
		/* 占据剩余70%空间 */
		min-height: 150px;
	}

	/* 调整签名容器布局 */
	:deep(.signature-container) {
		display: flex;

		flex: 1;
		height: 150px !important;
	}

	
	/* 日期部分保持右对齐 */
	.custom-dateText {
		margin: 20px 0;
		display: flex;
		justify-content: flex-end;
	}

	/* 响应式调整 */
	@media (max-width: 480px) {
		.label-text {
			flex-basis: 35%;
			font-size: 14px;
		}

		sign-input {
			min-height: 120px;
		}
	}

	.custom-text3 {
		padding-left: 10px;
		color: #0055ff;
		font-size: 16px;

		font-weight: bold;
		padding-right: 10px;

	}
</style>